package admin

import (
	"context"
	"errors"

	"server/internal/svc"
	"server/internal/types"
	"server/models"
	"server/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type SpecKeySaveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSpecKeySaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SpecKeySaveLogic {
	return &SpecKeySaveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SpecKeySaveLogic) SpecKeySave(req *types.SpecKeySaveReq) (resp string, err error) {
	if !utils.GetDataById(req.SpecTemplateID, &models.SpecTemplate{}) {
		return "", errors.New("规格模板不存在")
	}

	data := &models.SpecKey{
		Name:           req.Name,
		SpecTemplateID: req.SpecTemplateID,
	}

	if req.Id == 0 {
		// 创建新角色
		err = utils.InsertData(data)
	} else {
		// 更新现有角色
		err = utils.UpdateById(req.Id, data)
	}

	if err != nil {
		return "", err
	}

	return "操作成功", nil

}
